package com.cooperate.fly.controller;

import com.cooperate.fly.bo.Case;
import com.cooperate.fly.service.buildDB.BuildCaseService;
import com.cooperate.fly.util.SystemConstant;
import com.cooperate.fly.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by baobao on 2016/4/27.
 */
@Controller
@RequestMapping("/case")
@SessionAttributes(SystemConstant.USER_SESSION_KEY)
public class CaseDesignController extends BaseController {

    @Resource
    BuildCaseService buildCaseService;

    

    @RequestMapping(value="/add", method=RequestMethod.GET)
    @ResponseBody
    public Result add(Case _case) {
        Result res = buildCaseService.createCaseNode(_case);
        return res;
    }

    @RequestMapping(value="/update", method=RequestMethod.GET)
    @ResponseBody
    public Result update(Case _case) {
        //update
        Result res = buildCaseService.updateCaseNode(_case);
        return res;

    }

    @RequestMapping(value="/delete")
    @ResponseBody
    public Result delete(@RequestParam(value="id", required=true) int id) {
        Result res = buildCaseService.deleteCaseNodeById(id);
        return  res;
    }

    //访问某一数据库中的所有实例列表（含其所在分类）
    @RequestMapping(value = "/name/{schemaNames}", produces = "application/json;charset=utf-8",method = RequestMethod.GET,params = "action=get")
    @ResponseBody
    public String visitCases(@PathVariable String schemaNames){
        return buildCaseService.visitCases(schemaNames);
    }

    //访问给定数据库名及实例名的实例
    @RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}", produces = "application/json;charset=utf-8",method = RequestMethod.GET,params = "action=get")
    @ResponseBody
    public String visitCase(@PathVariable String schemaNames, @PathVariable String caseNames){
        return buildCaseService.visitCase(schemaNames, caseNames);
    }

    //visit case by caseId
    @RequestMapping(value = "/id/{caseId}", produces = "application/json;charset=utf-8",method = RequestMethod.GET,params = "action=get")
    @ResponseBody
    public String visitCase(@PathVariable String caseId){
        return buildCaseService.visitCaseById(Integer.parseInt(caseId));
    }

    //访问某数据库下、某实例下、某属性的值
    @RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}/field/name/{fieldNames}", produces = "application/json;charset=utf-8",method = RequestMethod.GET,params = "action=get")
    @ResponseBody
    public String visitField(@PathVariable String schemaNames, @PathVariable String caseNames, @PathVariable String fieldNames){
        return buildCaseService.visitField(schemaNames, caseNames, fieldNames);
    }

    //访问某数据库下、某实例下、某属性组的值
    @RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}/fields/name/{fieldNames}", produces = "application/json;charset=utf-8",method = RequestMethod.GET,params = "action=get")
    @ResponseBody
    public String visitFields(@PathVariable String schemaNames, @PathVariable String caseNames, @PathVariable String fieldNames){
        return buildCaseService.visitFields(schemaNames, caseNames, fieldNames);
    }

    @RequestMapping(value="/caseLink")
    @ResponseBody
    public Result getCasesToChoose(@RequestParam(value="checkIds", required=true) String checkIds, HttpSession session){
        Result res = new Result();
        res.setMessage(buildCaseService.getAllCasesToChoose(checkIds, session));
        return res;
    }
}
